package it.zqdszb.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.flyco.tablayout.SlidingTabLayout;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import it.zqdszb.R;
import it.zqdszb.entity.ChannelTabBean;
import it.zqdszb.library.adapter.pagerAdapter.MyViewPagerAdapter;
import it.zqdszb.library.api.HttpPostService;
import it.zqdszb.library.api.entitys.BaseResult;
import it.zqdszb.library.common.JConstant;
import it.zqdszb.library.ui.BaseLazyMVPFragment;
import it.zqdszb.library.utils.utils.RxDataTool;
import it.zqdszb.library.view.EmptyLayout;

/**
 * Created by GH on 2018-05-11.
 */

public class TvFragment extends BaseLazyMVPFragment {

	@BindView(R.id.toolBar)
	Toolbar mToolbar;
	@BindView(R.id.tabLayout)
	SlidingTabLayout mTabLayout;
	@BindView(R.id.viewpager)
	ViewPager mViewPager;

	@BindView(R.id.rl_toolbar)
	RelativeLayout rlToolbar;
	@BindView(R.id.appBarLayout)
	AppBarLayout appBarLayout;

	@BindView(R.id.emptyLayout)
	EmptyLayout emptyLayout;
	
	private List<ChannelTabBean>  tvTabData;
	private MyViewPagerAdapter viewPagerAdapter;


	@Nullable
	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return setView(inflater,R.layout.fragment_tv);
	}

	@Override
	protected void initView() {
		emptyLayout.showLoading();
		mToolbar.setTitle("");
		((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
		viewPagerAdapter = new MyViewPagerAdapter(getChildFragmentManager());
	}

	@Override
	protected void onFragmentFirstVisible() {
		super.onFragmentFirstVisible();
		Map<String, Object> params = new HashMap<>();
		params.put("groupId", JConstant.parentId);
		presenter.sendPost(HttpPostService.getTvTab, params, ChannelTabBean.class, true, false, null);
	}

	@Override
	public void onNext(BaseResult result) {
		super.onNext(result);
		tvTabData= (List<ChannelTabBean>) result.getResult();
		emptyLayout.showView();
		mTabLayout.setVisibility(!RxDataTool.isListEmpty(tvTabData)?View.VISIBLE:View.GONE);
		//插入推荐分组
		ChannelTabBean recommendChannel = new ChannelTabBean();
		recommendChannel.setName("推荐");
		recommendChannel.setId(-3);
		recommendChannel.setHas_node(0);
		tvTabData.add(0, recommendChannel);

		Iterator<ChannelTabBean> iter = tvTabData.iterator();
		while(iter.hasNext()){
			ChannelTabBean tabBean = iter.next();
			if(("CIBN".equals(tabBean.getName()))||("其他".equals(tabBean.getName()))){
				iter.remove();
			}
		}
		String[] mTitles = new String[tvTabData.size()];
		for (ChannelTabBean tabBean : tvTabData) {
			String tabName=tabBean.getName();
			int tabPositon=tvTabData.indexOf(tabBean);
			mTitles[tabPositon]=tabName;
			if("推荐".equals(tabName)){
				viewPagerAdapter.addFragment(RecommendTvFragment.newInstance(tabName), tabName);
				continue;
			}
			if(tabBean.getHas_node()== ChannelTabBean.HAS_CHILD_YES){
				viewPagerAdapter.addFragment(new RegionChannelFragment().newInstance(tabName,tabBean.getId()), tabName);
			}else{
				viewPagerAdapter.addFragment(new ChannelListFragment().newInstance(tabName,tabBean.getId()), tabName);
			}
		}
		mViewPager.setAdapter(viewPagerAdapter);
		mTabLayout.setViewPager(mViewPager,mTitles);

	}
}
